package com.datax.web.webbase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public abstract class AbstractCommonController {
	
	protected Object proccessResult(String resultStat, String mess, String callBack, Object data)
	  {
	    ProcessResult result = new ProcessResult(resultStat, mess, callBack);
	    result.setData(data);
	    return result;
	  }

	  protected Object handlerException(Exception e)
	  {
	    e.printStackTrace();
	    StringBuffer msg = new StringBuffer("");
	    StackTraceElement[] elements = e.getStackTrace();
	    if (elements != null) {
	      for (StackTraceElement ele : elements)
	        msg.append("\t" + ele.toString() + "\n");
	    }
	    else {
	      msg.append("null");
	    }

	    String mess = "";
	    if (StringUtils.hasText(e.getMessage())) {
	      mess = e.getMessage();
	    }
	    else if ((e.getCause() != null) && (StringUtils.hasText(e.getCause().getMessage()))) {
	      mess = e.getCause().getMessage();
	    }

	    return proccessResult("ERROR", mess, msg.toString());
	  }

	  protected Object proccessResult(ProcessStatHolder holder)
	  {
	    List messes = holder.getMess();
	    String mess = "处理未完成";
	    if ((messes != null) && (messes.size() > 0)) {
	      mess = (String)messes.get(messes.size() - 1);
	    }
	    ProcessResult result = new ProcessResult(holder.getResultFlag(), mess, messes);
	    holder.clearResultStatData();
	    return result;
	  }

	  protected Object proccessResult(ProcessResult result) {
	    return result;
	  }

	  protected Object proccessResult(String resultStat, String mess)
	  {
	    ProcessResult result = new ProcessResult(resultStat, mess);
	    return result;
	  }

	  protected Object proccessResult(String resultStat, String mess, Object data)
	  {
	    ProcessResult result = new ProcessResult(resultStat, mess);
	    result.setData(data);
	    return result;
	  }

	  protected Object proccessResult(String mess)
	  {
	    ProcessResult result = new ProcessResult();
	    result.setMess(mess);
	    return result;
	  }

	  protected List<Map<String, Object>> getDTOList(String jsonString)
	  {
	    if (!StringUtils.hasText(jsonString)) {
	      return new ArrayList();
	    }

	    List result = JSONArray.fromObject(jsonString);
	    return result;
	  }

	  protected Map<String, Object> getDTO(String jsonStr)
	  {
	    if (!StringUtils.hasText(jsonStr)) {
	      return new HashMap();
	    }
	    return JSONObject.fromObject(jsonStr);
	  }
}

